package net.youball.utils.mail;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailSend implements Runnable{
	/**

     *发送邮件的props文件

     */

    private final transient Properties props = System.getProperties();

	 /**
	  * 邮件服务器登录验证
	  * 	
	  * */

    private transient MailAuther authenticator;

 

    /**

     * 邮箱session

     */

    private transient Session session;

 
    /**

     * 初始化邮件发送器

     * 

     * @param smtpHostName

     *                SMTP邮件服务器地址

     * @param username

     *                发送邮件的用户名(地址)

     * @param password

     *                发送邮件的密码

     */
    public MailSend(final String smtpHostName, final String username,

            final String password) {

        init(username, password, smtpHostName);

    }
    /**

     * 初始化邮件发送器

     * 

     * @param username

     *                发送邮件的用户名(地址)，并以此解析SMTP服务器地址

     * @param password

     *                发送邮件的密码

     */

    public MailSend(final String username, final String password) {

    	 //通过邮箱地址解析出smtp服务器，对大多数邮箱都管用

    final String smtpHostName = "smtp." + username.split("@")[1];
    if(smtpHostName.indexOf("smtp.gmail.com")>=0)
    {
    	props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    	props.setProperty("mail.smtp.socketFactory.fallback", "false"); 
    	props.setProperty("mail.smtp.port", "465"); 
    	props.setProperty("mail.smtp.socketFactory.port", "465"); 
    }
    
    init(username, password, smtpHostName);

 

    }
    private void init(String username, String password, String smtpHostName) {
    	 // 初始化props

       
        props.put("mail.smtp.auth", "true");

        props.put("mail.smtp.host", smtpHostName);
        if(smtpHostName.indexOf("smtp.gmail.com")>=0)
        {
        	props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
        	props.setProperty("mail.smtp.socketFactory.fallback", "false"); 
        	props.setProperty("mail.smtp.port", "465"); 
        	props.setProperty("mail.smtp.socketFactory.port", "465"); 
        }

        // 验证
        authenticator = new MailAuther(username, password);

        // 创建session

        session = Session.getInstance(props, authenticator);
        //session = new Session(props, authenticator);

        }
    private  MimeMessage message;
    public void send(String recipient, String subject, Object content)

    throws AddressException, MessagingException {

    	// 创建mime类型邮件
		
		message = new MimeMessage(session);
		
		// 设置发信人
		
		message.setFrom(new InternetAddress(authenticator.getName()));
		
		// 设置收件人
		
		message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
		
		// 设置主题
		
		message.setSubject(subject);
		
		// 设置邮件内容
		
		message.setContent(content.toString(), "text/html;charset=utf-8");
		
		// 发送
		
	
	
	}
       
   /* public static void main(String[] args) throws AddressException, MessagingException {
		MailInfo info=new MailInfo("message");
		MailSend mail=new MailSend(info.getSmtp(),info.getName(),info.getPassword());
		mail.send("yan9jian@gmail.com", "验证成功", "哈哈哈哈，你好阿");
		//mail.run();
	}*/
    public static void sendOutEmail(String type,String theme,String context,String email){
    	try{
	    	MailInfo info=new MailInfo(type);
			MailSend mail=new MailSend(info.getSmtp(),info.getName(),info.getPassword());
	    	mail.send(email, theme, context);
	    	Thread t=new Thread(mail);
	    	t.start();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }
	public void run() {
		try {
			Transport.send(message);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
		
	}
    
  
}
